package com.bangcommunity.bbframe.common.utils.lang;

import com.bangcommunity.bbframe.common.enums.BaseEnum;
import com.bangcommunity.bbframe.common.enums.BaseNumEnum;

import java.util.ArrayList;
import java.util.List;

/**
 * enum相关工具
 * 
 * @author tanghc
 */
public class BaseEnumUtils {
    /**
     * 枚举对象列表批量转成num列表
     */
    public static <E extends BaseNumEnum> List<Integer> toNums(List<E> statuses) {
        List<Integer> nums = null;
        if (null != statuses) {
            nums = new ArrayList<>();
            for (E e : statuses) {
                nums.add(e.getNum());
            }
        }
        return nums;
    }

    public static <E extends BaseNumEnum> List<Integer> toNums(E... statuses) {
        List<Integer> nums = null;
        if (null != statuses) {
            nums = new ArrayList<>();
            for (E e : statuses) {
                nums.add(e.getNum());
            }
        }
        return nums;
    }

    /**
     * 枚举对象列表批量转成key列表
     */
    public static <E extends BaseEnum> List<String> toKeys(List<E> statuses) {
        List<String> keys = null;
        if (null != statuses) {
            keys = new ArrayList<>();
            for (E e : statuses) {
                keys.add(e.getKey());
            }
        }
        return keys;
    }

    /**
     * 枚举对象列表批量转成key列表
     */
    public static <E extends BaseEnum> List<String> toKeys(E... statuses) {
        List<String> keys = null;
        if (null != statuses) {
            keys = new ArrayList<>();
            for (E e : statuses) {
                keys.add(e.getKey());
            }
        }
        return keys;
    }

    public static <E extends BaseEnum> E fromBaseEnumKey(final Class<E> enumClass, String key) {
        List<E> enumList = EnumUtils.getEnums(enumClass);
        if (null != enumList) {
            for (E e : enumList) {
                if (StringUtils.equals(key, e.getKey())) {
                    return e;
                }
            }
        }
        return null;
    }

    public static <E extends BaseNumEnum> E fromBaseNumEnumKey(final Class<E> enumClass, int num) {
        List<E> enumList = EnumUtils.getEnums(enumClass);
        if (null != enumList) {
            for (E e : enumList) {
                if (e.getNum() == num) {
                    return e;
                }
            }
        }
        return null;
    }

    public static void main(String[] args) {
    }
}
